﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SocketLib
{
	public class IPMPack2
	{

		private long _version;
		private long _no;
		private long _command;
		private string _loginUser;
		private string _user;
		private string _host;
		private string _extra;
		private string _group;
		private byte[] _pack;
		private string[] _extraArray;
		public IPMPack2()
		{ }
		public IPMPack2(byte[] pack)
		{
			unpacked(pack);
		}
		public IPMPack2(string strCommand)
		{
			SetCommand(strCommand);
		}
		public long Version
		{
			get { return _version; }
			set { _version = value; }
		}

		public long No
		{
			get { return _no; }
			set { _no = value; }
		}

		public long Command
		{
			get { return _command; }
			set { _command = value; }
		}

		public string User
		{
			get { return _user; }
			set { _user = value; }
		}

		public string LoginUser
		{
			get { return _loginUser; }
			set { _loginUser = value; }
		}
		public string Host
		{
			get { return _host; }
			set { _host = value; }
		}
		public string Extra
		{
			get { return _extra; }
			set { _extra = value; }
		}
		public string Group
		{
			get { return _group; }
			set { _group = value; }
		}

		public byte[] Pack
		{
			get { return _pack; }
			set
			{
				_pack = value;
				unpacked(_pack);
			}
		}

		//public string[] ExtraArray
		//{
		//    get{return ExtraArray;}
		//}

		public override string ToString()
		{
			packed();
			return System.Text.Encoding.Default.GetString(_pack);
		}
		public byte[] GetBytes()
		{
			packed();
			return _pack;
		}
		private void packed()
		{
			StringBuilder strBuilder = new StringBuilder();
			strBuilder.Append(this.Version.ToString()).Append(":");
			strBuilder.Append(No.ToString()).Append(":");
			strBuilder.Append(LoginUser).Append(":");
			strBuilder.Append(Host).Append(":");
			strBuilder.Append(Command.ToString()).Append(":");
			//if (User != "")
			//{
			//    strBuilder.Append(User).Append("\0");
			//}
			//if (Group != "")
			//{
			//    strBuilder.Append(Group).Append("\0");
			//}
			strBuilder.Append(Extra);
			_pack = Encoding.Default.GetBytes(strBuilder.ToString());
		}

		public void SetCommand(string strMessage)
		{
			string[] s = strMessage.Split(':');

			try
			{
				this._version = long.Parse(s[0]);
			}
			catch
			{ }

			try
			{
				_no = long.Parse(s[1]);
			}
			catch { }

			try
			{
				_loginUser = s[2];
			}
			catch { }
			try
			{
				_host = s[3];
			}
			catch { }
			try
			{

				_command = long.Parse(s[4]);
			}
			catch { }
			try
			{
				_extra = s[5];
			}
			catch { }
			try
			{
				_extraArray = Extra.Split('\0');
				try
				{
					_user = _extraArray[0];
				}
				catch { }
				try
				{
					_group = _extraArray[1];
				}
				catch { }
			}
			catch { }
			if (string.IsNullOrEmpty(_user))
			{
				_user = _loginUser;
			}
		}
		public void unpacked(byte[] pack)
		{
			string[] s = System.Text.Encoding.Default.GetString(pack).Split(':');

			try
			{
				this._version = long.Parse(s[0]);
			}
			catch
			{ }

			try
			{
				_no = long.Parse(s[1]);
			}
			catch { }

			try
			{
				_loginUser = s[2];
			}
			catch { }
			try
			{
				_host = s[3];
			}
			catch { }
			try
			{

				_command = long.Parse(s[4]);
			}
			catch { }
			try
			{
				_extra = s[5];
			}
			catch { }
			//try
			//{
			//    _extraArray = Extra.Split('\0');
			//    try
			//    {
			//        _user = _extraArray[0];
			//    }
			//    catch { }
			//    try
			//    {
			//        _group = _extraArray[1];
			//    }
			//    catch { }
			//}
			//catch { }
			if (string.IsNullOrEmpty(_user))
			{
				_user = _loginUser;
			}
		}

		public void unpacked(byte[] pack, int index, int count)
		{
			byte[] bytes = new byte[count];
			Buffer.BlockCopy(pack, 0, bytes, index, count);
			unpacked(bytes);
		}


	}

}
